﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Cors;
using Brc_PaaS.Api.Common;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Core.Redis;
using Brc_PaaS.Common.Model.Redis;
using System.Collections.Generic;
using StackExchange.Redis;
using Brc_PaaS.Bll.System;
using System.ComponentModel.DataAnnotations;

namespace Brc_PaaS.Api.Controllers.Redis
{
    /// <summary>
    /// 操作Redis里面String类型控制器
    /// </summary>
    [Route("api/redis/[controller]/")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "Redis")]
    public class RedisStringController : RedisBaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="redis"></param>
        /// <param name="system"></param>
        public RedisStringController(IConfiguration configuration, Brc_PaaS.Bll.Redis.IRedis redis, ISystem system) : base(configuration, redis, system)
        {

        }


        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<BaseResponseModel> Post(RedisStringModel model)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && model != null)
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.IsSuccess = redisManager.StringSet(model.Key, model.Value, model.TimeSpan);
                }
            }
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }


        /// <summary>
        /// 获取单个值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ResponseModel<string>> Get([Required(ErrorMessage = "键不能为空")]string key)
        {
            ResponseModel<string> result = new ResponseModel<string>() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && !string.IsNullOrEmpty(key))
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.Data = redisManager.StringGet(key);
                }
            }
            result.IsSuccess = true;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }


    }
}
